Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.IO.Compression

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function DownloadPromoDat(ByVal dosyaAdi As String) As Byte()
        Dim folder As String = System.Configuration.ConfigurationManager.AppSettings("Promosyon") & "\" & dosyaAdi
        If File.Exists(folder) Then
            Dim fs As New FileStream(folder, FileMode.Open, FileAccess.Read, FileShare.Read)
            Dim buffer(fs.Length) As Byte
            fs.Read(buffer, 0, fs.Length)
            fs.Close()
            Return buffer
        Else
            Return Nothing
        End If
    End Function
    <WebMethod()> _
    Public Function SendBytes(ByVal filedirectory As String, ByVal byt() As Byte) As String
        Dim fs As New FileStream(filedirectory, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)
        fs.Write(byt, 0, byt.Length)
        fs.Close()
        Return "ok"
    End Function

    ''' <summary>
    ''' ExecuteString
    ''' </summary>
    ''' <param name="sql_server"></param>
    ''' <param name="database_name"></param>
    ''' <param name="sql_string"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <WebMethod()> _
    Public Function FillDataToString(ByVal sql_server As String, ByVal database_name As String, ByVal sql_string As String) As String
        Dim sifre As String = System.Configuration.ConfigurationManager.AppSettings("sa_sifre")
        Dim cnn_string As String = "SERVER=" & sql_server & ";DATABASE=" & database_name & ";Persist Security Info=False;User id=sa;Password=" & sifre
        Dim conn As New SqlConnection(cnn_string)
        conn.Open()
        Dim cmd As New SqlCommand(sql_string, conn)
        cmd.CommandTimeout = Integer.MaxValue
        Dim sonuc As String = cmd.ExecuteScalar()
        conn.Close()
        Return sonuc
    End Function

    '''' <summary>
    '''' ExecuteSql2
    '''' </summary>
    '''' <param name="sql_server"></param>
    '''' <param name="database_name"></param>
    '''' <param name="sql_string"></param>
    '''' <returns></returns>
    '''' <remarks></remarks>
    '<WebMethod()> _
    'Public Function FillDataToByte(ByVal sql_server As String, ByVal database_name As String, ByVal sql_string As String) As Byte()
    '    Dim sifre As String = System.Configuration.ConfigurationManager.AppSettings("sa_sifre")
    '    Dim cnn_string As String = "SERVER=" & sql_server & ";DATABASE=" & database_name & ";Persist Security Info=False;User id=sa;Password=" & sifre
    '    Dim conn As New SqlConnection(cnn_string)
    '    conn.Open()
    '    Dim dt As New DataTable
    '    Dim adp As New SqlDataAdapter(sql_string, conn)
    '    adp.SelectCommand.CommandTimeout = Integer.MaxValue
    '    adp.Fill(dt)
    '    conn.Close()
    '    Dim byt As Byte() '= SerializeObject(dt)
    '    Return byt
    'End Function

    ''' <summary>
    ''' ExecuteSql
    ''' </summary>
    ''' <param name="sql_server"></param>
    ''' <param name="database_name"></param>
    ''' <param name="sql_string"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <WebMethod()> _
    Public Function FillDataSet(ByVal sql_server As String, ByVal database_name As String, ByVal sql_string As String) As DataSet
        Dim sifre As String = System.Configuration.ConfigurationManager.AppSettings("sa_sifre")
        Dim cnn_string As String = "SERVER=" & sql_server & ";DATABASE=" & database_name & ";Persist Security Info=False;User id=sa;Password=" & sifre
        Dim conn As New SqlConnection(cnn_string)
        conn.Open()
        Dim ds As New DataSet
        Dim adp As New SqlDataAdapter(sql_string, conn)
        adp.SelectCommand.CommandTimeout = Integer.MaxValue
        adp.Fill(ds)
        conn.Close()
        Return ds
    End Function

    ''' <summary>
    ''' StringCevir
    ''' </summary>
    ''' <param name="sql_server"></param>
    ''' <param name="database_name"></param>
    ''' <param name="sql_string"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <WebMethod()> _
    Public Function FillSqlToByte(ByVal sql_server As String, ByVal database_name As String, ByVal sql_string As String) As Byte()
        Dim sifre As String = System.Configuration.ConfigurationManager.AppSettings("sa_sifre")
        Dim cnn_string As String = "SERVER=" & sql_server & ";DATABASE=" & database_name & ";Persist Security Info=False;User id=sa;Password=" & sifre
        Dim conn As New SqlConnection(cnn_string)
        conn.Open()
        Dim cmd As New SqlCommand
        cmd.Connection = conn
        cmd.CommandText = sql_string
        cmd.CommandTimeout = Integer.MaxValue

        Dim sb As New System.Text.StringBuilder
        Dim rd As SqlDataReader
        rd = cmd.ExecuteReader

        Dim dt As New DataTable
        dt = rd.GetSchemaTable
        For k As Integer = 0 To dt.Rows.Count - 1
            sb.Append(dt.Rows(k)("ColumnName") & "?" & dt.Rows(k)("DataTypeName") & vbCrLf)
        Next

        Dim line As String = ""
        Dim cl As Integer = rd.FieldCount
        While rd.Read
            For i As Integer = 0 To cl - 1
                If i < cl - 1 Then
                    line &= rd(i) & ";"
                Else
                    line &= rd(i)
                End If
            Next
            sb.Append(line & vbCrLf)
            line = ""
        End While
        conn.Close()
        Dim byt As Byte() = System.Text.Encoding.Default.GetBytes(sb.ToString)
        Return Compress(byt)
    End Function

    Public Function Compress(ByVal bytData As Byte()) As Byte()
        Try
            Dim ms As New MemoryStream()
            Dim s As Stream = New GZipStream(ms, CompressionMode.Compress)
            s.Write(bytData, 0, bytData.Length - 1)
            s.Close()
            Dim compressedData As Byte() = ms.ToArray
            Return compressedData
        Catch
            Return Nothing
        End Try
    End Function


End Class
